package org.wenhuayuan.project.main.fragment.home

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.layout_home_op_grid_item.view.*
import org.wenhuayuan.library.util.HiDisplayUtil
import org.wenhuayuan.project.common.util.AppGlobals
import org.wenhuayuan.project.common.view.loadUrl
import org.wenhuayuan.project.main.R
import org.wenhuayuan.project.main.model.Subcategory
import org.wenhuayuan.uilibrary.hiitem.HiDataItem
import org.wenhuayuan.uilibrary.hiitem.HiViewHolder

class GridItem(private val list:List<Subcategory>)
//    : HiDataItem<List<Subcategory>, HiViewHolder>(list){
    : HiDataItem<List<Subcategory>, RecyclerView.ViewHolder>(list){
    override fun onBindData(holder: RecyclerView.ViewHolder, position: Int) {
        val context = holder.itemView.context
        val gridView = holder.itemView as RecyclerView
        gridView.adapter = GridAdapter(context, list)
    }

    override fun getItemView(parent: ViewGroup): View? {
        val gridView = RecyclerView(parent.context)
        val params = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
            RecyclerView.LayoutParams.WRAP_CONTENT)
        params.bottomMargin = HiDisplayUtil.dp2px(10f)
        gridView.layoutManager = GridLayoutManager(parent.context, 5)
        gridView.layoutParams = params
        gridView.setBackgroundColor(Color.WHITE)
        return gridView
    }

    inner class GridAdapter(val context: Context, val list:List<Subcategory>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HiViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_home_op_grid_item, parent, false)

            return HiViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val subcategory = list[position]
            holder.itemView.item_image.loadUrl(subcategory.subcategoryIcon)
            holder.itemView.item_title.text = subcategory.subcategoryName
//            holder.item_image.loadUrl(subcategory.subcategoryIcon)
//            holder.item_title.text = subcategory.subcategoryName

            holder.itemView.setOnClickListener {
                // 跳转到子分类列表中
                Toast.makeText(AppGlobals.get(), "跳转到子分类列表中", Toast.LENGTH_SHORT).show()
            }

        }

    }
}